<template>
    <div v-if="userstore.information.Name">
        <h3>显示</h3>

        <input type="button" value="批量删除" class="btn btn-danger" @click="delall">
        <input type="button" value="批量开始" class="btn btn-danger" @click="updall(0)">
        <input type="button" value="批量结束" class="btn btn-danger" @click="updall(1)">


        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>
                        <input type="checkbox" @click="headall" v-model="checked">
                    </td>
                    <td>Id</td>
                    <td>药品名称</td>
                    <td>科室</td>
                    <td>用药日期</td>
                    <td>病人姓名</td>
                    <td>病房号码</td>
                    <td>用药状态</td>
                    <td>配药医生</td>
                    <td>服务</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.list1">
                    <td>
                        <input type="checkbox" :value="item.Id" @change="bodyall" v-model="ids">
                    </td>
                    <td>{{ item.Id }}</td>
                    <td>{{ item.Name }}</td>
                    <td>{{ item.OfficeName }}</td>
                    <td>{{ item.CreateTime }}</td>
                    <td>{{ item.PeopleName }}</td>
                    <td>{{ item.Number }}</td>
                    <td>{{ item.State==0?"已完成":"未开始" }}</td>
                    <td>{{ item.Yisheng }}</td>
                    <td>{{ item.Fuwu}}</td>
                    <td>
                        <input type="button" value="删除" class="btn btn-danger" @click="(item.Id)">
                        <input type="button" value="修改" class="btn btn-success" @click="upd(item.Id)">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div v-else>
        <h3>登录</h3>
        <input type="button" value="登录" class="btn btn-success" @click="login">

    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import moment from 'moment';
import { ref,reactive,onMounted } from 'vue';
import { useRoute,useRouter } from 'vue-router';
import { ssrLooseContain } from 'vue/server-renderer';
const route=useRoute();
const router=useRouter();

import { useUserStore } from '@/router/user';
const userstore:any=useUserStore();

let data:any=reactive({
    list:[],
    list1:[],
    totalCount:0,
    totalPage:0
})
let can:any=reactive({
    pageIndex:1,
    pageSize:2,
    name:"",
    officeId:""
})
let checked=ref();
let ids=ref([])
onMounted(()=>{
    getoffice();
    getyao();
})

const upd=(id:any)=>{
    router.push({
        path:('/yaoupd'),
        query:{
            id:id
        }
    })
}

const login=()=>{
    router.push('/login')
}


const delall=()=>{
    axios({
        method:"delete",
        url:"/api/Month/DelAll",
        params:{
            ids:ids.value.toString()
        }
    })
    .then(res=>{
        if(res.data>0){
            alert('删除成功')
            ids.value=[];
            checked.value="";
            getyao();
        }
    })
    
}
const updall=(state:any)=>{
    axios({
        method:"get",
        url:"/api/Month/UpdAll",
        params:{
            ids:ids.value.toString(),
            state:state
        }
    })
    .then(res=>{
        if(res.data>0){
            alert('修改成功')
            ids.value=[];
            checked.value="";
            getyao();
        }
    })
    
}

//全选全不选
const headall=()=>{
    checked.value=!checked.value;
    if(checked.value==true){
        ids.value=data.list1.map((item:any)=>{
            return item.Id
        })
    }else
    {
        ids.value=[]
    }
}
//反选
const bodyall=()=>{
    if(ids.value.length==data.list1.length){
        checked.value=true;
    }else{
        checked.value=false;
    }
}

//获取药品列表
const getyao=()=>{
    axios({
        method:"get",
        url:"/api/Month/ShowYao",
        params:can
    })
    .then(res=>{
        console.log(res)
        data.list1=res.data.list,
        data.totalCount=res.data.totalCount,
        data.totalPage=res.data.totalPage
    })
}


//科室列表
const getoffice=()=>{
    axios({
        method:"get",
        url:"/api/Month/ShowOffice"
    })
    .then(res=>{
        console.log(res)
        data.list=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}


</script>

<style scoped>

</style>